#include <stdio.h>
#include <string.h>
#include <libgen.h>

#include "./src/lt_net.h"
#include "./src/lt_unit.h"
#include "./src/lt_types.h"
#include "./src/lt_logger.h"
#include "./src/lt_string.h"
#include "./src/lt_md5.h"
#include "./src/lt_host_info.h"

typedef lt_boolean (*tool_func)();

void init(int argc, char **args)
{
    //char *dir_path = dirname(args[0]);
    //update_cur_dir(dir_path);
    logger_init("/var/log/linux_tools");
}

lt_boolean process_mode(tool_func tool, int argc, char **args)
{
    return tool(argc, args);
}

void help()
{
    printf("------------------------------------------\n");
    printf("------------ Linux Viewer HELP -----------\n");
    printf("------------------------------------------\n");
    printf("  %-50s\n", "Function List");
    printf("  %-50s\n", "1. INFO");
    printf("  %-50s\n", "    a. HOST");
    printf("  %-50s\n", "2. NET");
    printf("  %-50s\n", "    a. TCP Server (ts) with listen IP and Port");
    printf("  %-50s\n", "    b. TCP Client (tc) with server IP and Port");
    printf("  %-50s\n", "    c. UDP Server (us) with listen IP and Port");
    printf("  %-50s\n", "    d. UDP Client (uc) with server IP and Port");
    printf("  %-50s\n", "3. FC (file calculate)");
    printf("  %-50s\n", "    a. MD5 Calculate (md5) with absolute file Path");
    printf("------------------------------------------\n");
    printf("------------------------------------------\n");
}

int main(int argc, char **args)
{
    init(argc, args);

    to_lower_case(args[1]);

    tool_func tool = NULL;
    log_info("choose_tools with order %d", args[1]);
    if ( argc <= 1 || strstr(args[1], "help") )
    {
        help();
    }
    else if ( !strcmp("info", args[1]) )
    {
        if ( argc <= 2 )
        {
            tool = static_genernal_info;
        }
        else
        {
            to_lower_case(args[2]);
            if ( !strcmp("host", args[2]) )
            {
                tool = static_genernal_info;
            }
            else
            {
                printf("Error: Second Option %s Is Not Support!\n", args[2]);
                log_error("Error: Second Option %s Is Not Support!", args[2]);
                help();
            }
        }
    }
    else if ( !strcmp("net", args[1]) )
    {
        if ( argc <= 2 )
        {
            printf("Please enter second option and some necessary parameters\n");
        }
        else
        {
            to_lower_case(args[2]);
            if ( !strcmp("ts", args[2]) )
            {
                tool = tcp_server_epoll;
            }
            else if ( !strcmp("tc", args[2]) )
            {
                tool = tcp_client;
            }
            else
            {
                printf("Error: Second Option %s Is Not Support!\n", args[2]);
                log_error("Error: Second Option %s Is Not Support!", args[2]);
                help();
            }
        }
    }
    else if ( !strcmp("fc", args[1]))
    {
        if ( argc <= 2 )
        {
            printf("Please enter second option and some necessary parameters\n");
        }
        else
        {
            to_lower_case(args[2]);
            if ( !strcmp("md5", args[2]) )
            {
                tool = md5_cal;
            }
            else
            {
                printf("Error: Second Option %s Is Not Support!\n", args[2]);
                log_error("Error: Second Option %s Is Not Support!", args[2]);
                help();
            }
        }
    }
    else
    {
        log_error("ERROR Unknow Choice With Order %d", args[1]);
        log_error("ERROR Unknow Choice\n");
    }

    if ( tool != NULL )
    {
        if ( !process_mode(tool, argc - 2, args + 2) )
        {
            log_error("Execute Failed with Order %d", args[1]);
        }
    }
    else
    {
        return -1;
    }
    return 0;
}